using System;
using System.Collections.Generic;
using System.Text;
using ClientGUI.Modules.Drawing;
using System.Drawing;
using System.Xml;
using ClientGUI.Util;

namespace ClientGUI
{
    /// <summary>
    /// Draw a simple square, usefull as a container control.
    /// 
    /// This is likely to be replaced by more complex drawing classes in
    /// the future, but serves as a useful stub during development.
    /// </summary>
	class SimpleSquare:Module
	{
        protected DisplayArea myDisplay;
        protected DisplayArea mySelectedDisplay;
        protected DisplayArea myHoverDisplay;
        protected Boolean hasBeenSized;

		XmlNode my_config;
		Color my_bg;
		public SimpleSquare(XmlNode doc, Module parent, 
			DisplayDocument host, XmlNode config, TagConfig tagconfig): 
			base(doc, parent, host, tagconfig)
		{
			my_config = config;
			String bg_color = config.SelectSingleNode("Background").
				Attributes.GetNamedItem("color").Value;

			int red= Convertors.HexToInt(bg_color.Substring(1, 2).ToUpper());
			int green= Convertors.HexToInt(bg_color.Substring(3, 2).ToUpper());
			int blue= Convertors.HexToInt(bg_color.Substring(5, 2).ToUpper());
			my_bg = Color.FromArgb(red, green, blue);
            //my_bottom_buffer = 5;
		}


        public override DisplayArea GetDisplayArea()
        {
            if (!hasBeenSized)
            {
                return new BlankDisplayArea();
            }
            if (selected)
            {
#if DEBUG
                Console.WriteLine("DisplayArea.GetDisplay: Getting Bold Display");
#endif
                if (mySelectedDisplay == null) MakeDisplayArea();
                return mySelectedDisplay;
            }
            else if (hoovered)
            {
                if (myHoverDisplay == null) MakeDisplayArea();
                return myHoverDisplay;
            }
            else
            {
                if (mySelectedDisplay == null) MakeDisplayArea();
                return myDisplay;
            }
        }

		
		public override void Space(int x)
		{
            base.Space(x);
            hasBeenSized = true;
		}

        private void MakeDisplayArea()
        {
            //TODO: Put this in the configuration file
            Color hover = Color.FromArgb(my_bg.R - 15, my_bg.G - 15, my_bg.B - 15);
            Color select = Color.FromArgb(my_bg.R - 30, my_bg.G - 30, my_bg.B - 30);
            myHoverDisplay = new RectangleDisplayArea(hover, new Rectangle(0, 0, my_width, my_height), new Pen(Color.DarkGray, 2f), new Pen(Color.LightGray, 0.1f), new RectangleVisible(false, false, false, false), new RectangleVisible(true, true, false, true));
            myDisplay = new RectangleDisplayArea(my_bg, new Rectangle(0, 0, my_width, my_height), new Pen(Color.DarkGray, 2f), new Pen(Color.LightGray, 0.1f), new RectangleVisible(false, false, false, false), new RectangleVisible(false, false, false, false));
            mySelectedDisplay = new RectangleDisplayArea(select, new Rectangle(0, 0, my_width, my_height), new Pen(Color.DarkGray, 2f), new Pen(Color.LightGray, 0.1f), new RectangleVisible(false, true, false, true), new RectangleVisible(true, false, false, false));

        }

        public override void Dispose()
        {
            base.Dispose();
            myHoverDisplay.Dispose();
            myDisplay.Dispose();
            mySelectedDisplay.Dispose();
        }
	}
}
